package com.teamelite.exmgmt;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class myAdapter extends BaseAdapter {
	private ArrayList<arrList> listData;
	private LayoutInflater myInflater;
	public String adapterFor;
	
	public myAdapter(Context context,ArrayList<arrList> data,String adapterFor) {
		// TODO Auto-generated constructor stub
		listData = data;
		myInflater = LayoutInflater.from(context);
		this.adapterFor = adapterFor;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return listData.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(this.adapterFor.equals("Pengguna")){
			// Set Layout Holder
			ViewHolder holder;
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row, null);
				holder = new ViewHolder();
				holder.txtDesc = (TextView) convertView.findViewById(R.id.txtDesc);
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			cFunction f = new cFunction();
			holder.txtDesc.setText("Gender: "+f.convertGenderFromDB(listData.get(position).gender)+", " +
					"Umur: "+listData.get(position).umur+", Tinggi: "+listData.get(position).tinggi+
					", Berat: "+listData.get(position).berat);
			holder.txtTitle.setText(listData.get(position).nama);
		}else if(this.adapterFor.equals("Aktivitas")){
			// Set Layout Holder
			VHListActivity holder;
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row_lstactivity, null);
				holder = new VHListActivity();
				holder.txtDate = (TextView) convertView.findViewById(R.id.txtDate);
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
				convertView.setTag(holder);
			}else{
				holder = (VHListActivity) convertView.getTag();
			}

			String[] datetime = listData.get(position).date.split(" ");
			holder.txtDate.setText("Started On: "+datetime[0].split("-")[2]+"-"+datetime[0].split("-")[1]+"-"+datetime[0].split("-")[0]+", "+datetime[1].split(":")[0]+":"+datetime[1].split(":")[1]+":"+datetime[1].split(":")[2].split("\\.")[0]);
			holder.txtTitle.setText(listData.get(position).nama_activity);
		}else if(this.adapterFor.equals("AktivitasYear")){
			// Set Layout Holder
			VHListActivity holder;
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row_oneitem, null);
				holder = new VHListActivity();
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
				convertView.setTag(holder);
			}else{
				holder = (VHListActivity) convertView.getTag();
			}
			
			holder.txtTitle.setText(listData.get(position).year_activitydo);			
		}else if(this.adapterFor.equals("AktivitasMonth")){
			// Set Layout Holder
			VHListActivity holder;
			cFunction f;
			f = new cFunction();
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row_oneitem, null);
				holder = new VHListActivity();
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
				convertView.setTag(holder);
			}else{
				holder = (VHListActivity) convertView.getTag();
			}
			
			holder.txtTitle.setText(f.convertMonthToName(Integer.parseInt(listData.get(position).month_activitydo)));			
		}else if(this.adapterFor.equals("AktivitasGroup")){
			// Set Layout Holder
			VHListActivity holder;
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row_oneitem, null);
				holder = new VHListActivity();
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
				convertView.setTag(holder);
			}else{
				holder = (VHListActivity) convertView.getTag();
			}
			holder.txtTitle.setText(listData.get(position).nama_activity);			
		}else if(this.adapterFor.equals("MenuAktivitas")){
			// Set Layout Holder
			imgHolder holder;
			if(convertView == null) {
				convertView = myInflater.inflate(R.layout.row_menuactivity, null);
				holder = new imgHolder();
				holder.img1 = (ImageView) convertView.findViewById(R.id.imgView);
				convertView.setTag(holder);
			}else{
				holder = (imgHolder) convertView.getTag();
			}

			holder.img1.setImageResource(listData.get(position).imgid);
		}
		return convertView;
	}
	
	static class ViewHolder {
		TextView txtTitle;
		TextView txtDesc;
	}
	
	static class imgHolder {
		ImageView img1;
	}
	
	static class VHListActivity{
		TextView txtTitle;
		TextView txtDate;
		TextView txtTime;
	}
}
